//
//  VipCenterVC.swift
//  LeTu
//
//  Created by gy on 2023/12/7.
//

import UIKit

class VipCenterVC: BaseViewController {

    private var viewModel = VipCenterVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
                
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        kAppThemeManager.transparencyNavBar()
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back_white")?.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(backAction))

        getData()
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        kAppThemeManager.unTransparencyNavBar()
    }
    private func getData() {
        
        viewModel.getVipPriceList { [weak self] dialog in
            
            guard let strongSelf = self else { return }
            DispatchQueue.main.async {
                if dialog.code == 200 {
                    strongSelf.vipTableView.reloadData()
                } else {
                    strongSelf.showMessage(message: dialog.message)
                }
            }
        }
        
        kUserManager.getUserInfomation { [weak self] dialog in
            
            guard let strongSelf = self else { return }
            DispatchQueue.main.async {
                if dialog.code == 200 {
                    strongSelf.vipTableView.reloadData()
                } else {
                    strongSelf.showMessage(message: dialog.message)
                }
            }
        }
        
    }
    
    // MARK: - configUI
    func configUI() {
        
        navIsHidden = false
        title = "会员中心".localized
        view.backgroundColor = .app_inputViewBackColor
        
        view.addSubview(imgView)
        view.addSubview(vipTableView)
        
        imgView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(180)
        }
        vipTableView.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(Size.navigationBarHeight)
        }
        
        vipTableView.delegate = self
        vipTableView.dataSource = self
        vipTableView.register(cellClass: VipInfoTableViewCell.self)
        vipTableView.register(cellClass: VipPromptTableViewCell.self)
        
    }
    private lazy var imgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.image = UIImage(named: "vip_top_background")
        return imgView
    }()
    private lazy var vipTableView: UITableView = {
        let vipTableView = UITableView()
        vipTableView.tableFooterView = UIView()
        vipTableView.showsVerticalScrollIndicator = false
        vipTableView.backgroundColor = .clear
        return vipTableView
    }()

}

extension VipCenterVC: TransferEventReceivable {

    enum TransferEvent: TransferEventType {
        case openVip(priceModel: VipPirceModel)
    }
    
    func receive(event: TransferEventType) {
        switch event {
        case VipCenterVC.TransferEvent.openVip(let priceModel):
            startloading()
            ApplePayManager.shared.requesProductInfo(productId: priceModel.productId, purchaseType: Int64(priceModel.type), orderMoney: Float(priceModel.productPrice)) { [weak self] in
                guard let strongSelf = self else { return }
                DispatchQueue.main.async {
                    strongSelf.stoploading()
                    strongSelf.getData()
                }
            } requestProductSuccess: {} failure: { [weak self] in
                guard let strongSelf = self else { return }
                DispatchQueue.main.async {
                    strongSelf.stoploading()
                    strongSelf.showMessage(message: "支付失败，请重试！")
                }
            }

        default:
            break
        }
    }
    
}

extension VipCenterVC: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footer = UIView()
        footer.backgroundColor = .app_inputViewBackColor
        return footer
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return VipInfoTableViewCell.getCellHeight()
        }
        return VipPromptTableViewCell.getCellHeight()
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(VipInfoTableViewCell.self, indexPath: indexPath)
            cell.updateUserInfo()
            cell.setVipPrices(viewModel.pricesArr)
            return cell
        }
        let cell = tableView.dequeueReusableCell(VipPromptTableViewCell.self, indexPath: indexPath)
        return cell
    }
    
}

